package com.cxx.purchasecharge.core.model.persistence;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.cxx.purchasecharge.core.model.BaseModel;

/**
 * 系统权限
 */
@Entity
@Table (name = "pc_authority")
public class Authority extends BaseModel
{
    private static final long serialVersionUID = 1L;

    private long id;
    private String name;
    private String comment;
    private Authority parent;
    private boolean enable = true;
    private boolean system = false;

    private List <Resource> resources;
    private List <Role> roles;

    public Authority ()
    {
    }

    public Authority (String name)
    {
        this.name = name;
    }

    public Authority (String name, List <Resource> resources)
    {
        this (name);
        this.resources = resources;
    }

    @Id
    @GeneratedValue (strategy = GenerationType.AUTO)
    public long getId ()
    {
        return id;
    }

    public void setId (long id)
    {
        this.id = id;
    }

    @Column (nullable = false, unique = true)
    public String getName ()
    {
        return name;
    }

    public void setName (String name)
    {
        this.name = name;
    }

    @Column (length = 2048)
    public String getComment ()
    {
        return comment;
    }

    public void setComment (String comment)
    {
        this.comment = comment;
    }

    @ManyToMany (mappedBy = "authorities")
    public List <Role> getRoles ()
    {
        return roles;
    }

    public void setRoles (List <Role> roles)
    {
        this.roles = roles;
    }

    @ManyToMany (fetch = FetchType.EAGER)
    @JoinTable (name = "pc_authority_resource", joinColumns =
    { @JoinColumn (name = "authority_id", referencedColumnName = "id") }, inverseJoinColumns =
    { @JoinColumn (name = "resource_id", referencedColumnName = "id") })
    public List <Resource> getResources ()
    {
        return resources;
    }

    public void setResources (List <Resource> resources)
    {
        this.resources = resources;
    }

    @ManyToOne
    @JoinColumn (name = "parent_id")
    public Authority getParent ()
    {
        return parent;
    }

    public void setParent (Authority parent)
    {
        this.parent = parent;
    }

    public boolean isEnable ()
    {
        return enable;
    }

    public void setEnable (boolean enable)
    {
        this.enable = enable;
    }

    public boolean isSystem ()
    {
        return system;
    }

    public void setSystem (boolean system)
    {
        this.system = system;
    }
}
